package com.guiji.quartz.domain;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import cn.wanghaomiao.xpath.exception.NoSuchAxisException;
import cn.wanghaomiao.xpath.exception.NoSuchFunctionException;
import cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.guiji.common.utils.spring.SpringUtils;
import com.guiji.quartz.formatter.DataTemplateFormatter;
import com.guiji.quartz.service.ISpiderNewsService;
import com.guiji.quartz.service.RedisService;
import com.guiji.quartz.task.DataSaveUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: gaoX
 * @create: 2021-09-03 10:57
 **/
public class Dongmanxingkong {
	private static final Logger log = LoggerFactory.getLogger(DongmanXinwen.class);

	public void mainMethod() throws NoSuchFunctionException, XpathSyntaxErrorException, NoSuchAxisException {
		List<String> entry = getEntry();
		String resp = null;
		try {
			HttpResponse response = HttpRequest.get(entry.get(0))
				.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
				.header("Accept-Encoding", "gzip, deflate")
				.setConnectionTimeout(15000)
				.execute();
			resp = response.body();
		} catch (HttpException e) {
			return;
		}
		List<String> resUrl = getJsUrl(resp);
		getContent(resUrl);
	}

	private static void getContent(List<String> listContent) throws NoSuchFunctionException, XpathSyntaxErrorException, NoSuchAxisException {
		for (String url : listContent) {
			String doc = null;
			JXDocument jxDocument = null;
			try {
				HttpResponse response = HttpRequest.get(url)
					.setConnectionTimeout(15000)
					.execute();
				doc = response.body();
				jxDocument = new JXDocument(doc);
			} catch (HttpException e) {
				continue;
			}
			String title = null;
			try {
				title = (String) jxDocument.sel("//h1/text()").get(0);
			} catch (Exception e) {
				System.out.println("标题错误-------------" + url + "-------------------标题错误");
				continue;
			}
			StringBuffer content = new StringBuffer();

			try {
				jxDocument.sel("//div[@class='MidL_con']//p/text()").forEach(s -> {
					content.append(s+"$$$");
				});
			} catch (Exception e) {
				continue;
			}

			List<Object> imgs = jxDocument.sel("//img[@class='picact']/@src");

			Document document = Jsoup.parse(doc);
			DataTemplateFormatter dataTemplateFormatter = new DataTemplateFormatter();

			Date date = null;
			try {
				date = dataTemplateFormatter.format(document.getElementsByClass("detail").toString().substring(41,60));
			} catch (Exception e) {
				continue;
			}

			if (content.length() > 5000 || content.toString().replaceAll("\\$|[\\s\\p{Zs}]","").length() < 20 || imgs.size() > 20) {
				continue;
			}
			try {
				DataSaveUtil.saveData(title, content.toString(), imgs, url, date, "https://acg.gamersky.com/", "动漫星空");
				log.info(Thread.currentThread().getName() + "-----------------动漫星空----------------");
			} catch (Exception e) {
				continue;
			}
		}
	}

	private static List<String> getJsUrl(String content) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String s = sdf.format(new Date());
		String[] ss = sdf.format(new Date()).split("-");
		String year = ss[0];
		String mon = ss[1];
		String day = ss[2];

		List<String> res = new ArrayList<>();
		String pattern = "https://acg.gamersky.com/news/" + year+ mon + ".*?shtml";//爬取当月的新闻
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(content);
		while (m.find()) {
			res.add(m.group());
		}
		return res;
	}


	public static List<String> getEntry() {
		List<String> listEntry = new ArrayList<>();
		listEntry.add("https://acg.gamersky.com/");
		return listEntry;
	}
}
